package com.shawn.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.shawn.commons.utils.CommentType;
import com.shawn.commons.utils.RC;
import com.shawn.model.entity.UserInfo;
import com.shawn.model.mongo.Movement;
import com.shawn.model.vo.MovementsVo;
import com.shawn.model.vo.PageResult;
import com.shawn.model.vo.VisitorsVo;
import com.shawn.server.interceptor.ThreadContext;
import com.shawn.server.service.FreezeService;
import com.shawn.server.service.MovementService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
private MovementService movementService;

@Autowired
private FreezeService freezeService;

    /**
     * 获取所有访客信息
     *
     * @return {@link ResponseEntity}
     */
    @GetMapping("/visitors")
    public ResponseEntity getVisitors(){
        Long userId = ThreadContext.getUserId();
        List<VisitorsVo> voList = movementService.getVisitors(userId);
        return ResponseEntity.ok(voList);
    }

    /**
     * 发布动态
     *
     * @param imageContent 图像内容
     * @param movement     运动
     * @return {@link ResponseEntity}
     * @throws IOException ioexception
     */
    @PostMapping
    public ResponseEntity publishDynamic(MultipartFile[] imageContent, Movement movement) throws IOException {
        freezeService.checkFreeze(ThreadContext.getUserId(),RC.FREEZING_PUBLISH_MOVEMENT);
        //发布动态
        movementService.publishDynamic(imageContent,movement);
        return ResponseEntity.ok("动态已发布!");
    }

    /**
     * 分页获取自己所有动态
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param userId   用户id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/all")
    public ResponseEntity getAllDynamics(@RequestParam(defaultValue = "1") Integer page,@RequestParam(name ="pagesize" ,defaultValue = "10") Integer pageSize,Long userId){
        //分页查询动态信息
        PageResult<MovementsVo> pageResult = movementService.getAllDynamics(page,pageSize,userId);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 得到朋友动态
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param userId   用户id
     * @return {@link ResponseEntity}
     */
    @GetMapping
    public ResponseEntity getFriendsDynamics(@RequestParam(defaultValue = "1") Integer page,@RequestParam(name ="pagesize" ,defaultValue = "10") Integer pageSize,Long userId){
        //放置userId为空
        if (userId==null) {userId = ThreadContext.getUserId();}
        //通过发布时间,分页查询时间线表,查询该id所有好友动态信息
        PageResult<MovementsVo> pageResult = movementService.getFriendsDynamics(page,pageSize,userId);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐动态
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @return {@link ResponseEntity}
     */
    @GetMapping("/recommend")
    public ResponseEntity recommendDynamics(@RequestParam(defaultValue = "1") Integer page,@RequestParam(name ="pagesize" ,defaultValue = "10") Integer pageSize){
        //todo 根据redis内pid查询该用户的推荐动态
        PageResult<MovementsVo> pageResult = movementService.getRecommendDynamics(page,pageSize);
        //
        return ResponseEntity.ok(pageResult);
    };

    /**
     * 得到单一动态
     *
     * @param id id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/{id}")
    public ResponseEntity getSingleDynamic(@PathVariable String id){
       MovementsVo movementsVo =  movementService.getSingleDynamic(id);
       return ResponseEntity.ok(movementsVo);
    }


    /**
     * 点赞动态
     *
     * @param id 动态id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id ){
        Integer likedCount = movementService.likeOrDislike(id, CommentType.LIKE.getType());
        return ResponseEntity.ok(likedCount);
    }
    /**
     * 取消点赞动态
     *
     * @param id id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable String id ){
        Integer likedCount = movementService.likeOrDislike(id,CommentType.DISLIKE.getType());
        return ResponseEntity.ok(likedCount);
    }
    /**
     * 喜欢动态
     *
     * @param id id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable String id ){
        Integer likedCount = movementService.loveOrNotLove(id,CommentType.IS_LOVE.getType());
        return ResponseEntity.ok(likedCount);
    }
    /**
     * 取消喜欢动态
     *
     * @param id id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity notLove(@PathVariable String id ){
        Integer likedCount = movementService.loveOrNotLove(id,CommentType.UN_LOVE.getType());
        return ResponseEntity.ok(likedCount);
    }
}
